
#include <stdlib.h>
#include <string.h>

#include "../../1.Application/Include/APP_WorkingNormalState.h"
#include "../../3.Library/Include/general.h"

#define	__LEDDISPLAY__
#include "../../2.Component/Include/Cmp_Display_Service.h"
#undef	__LEDDISPLAY__

#include "../../2.Component/Include/Cmp_KeyScan.h"
#include "../../2.Component/Include/Cmp_SetError.h"

#include "../../3.Library/Include/Int.h"
#include "../../3.Library/Include/Lib_UartComm.h"
#include "../../3.Library/Include/Lib_CommSubMicom.h"
#include "../../3.Library/Include/Lib_SensingADC.h"
#include "../../3.Library/Include/Lib_EEPROM.h"

#include "../../4.Driver/Include/WROS_Register.h"


#include "../../6.VariableSource/Include/Vable_ModelInformation.h"

UINT8 gu8Temp_Data[4];	// Temp led output data

void Cmp_Dsp_Setting_Normal(UINT8,UINT16);
void Cmp_Dsp_Setting_Error();
void Cmp_Dsp_Displaying(void);
UINT8 Lamp1sBlink(UINT8,UINT8);
UINT8 NumbertoChar(UINT8);

const UINT8 Number_pattern[16] = 
{	// 18:88 LED Numeric,Character Display
	// Number            0x30
	0x3f, 0x06, 0x5b, 0x4f, 0x66,		// 0, 1, 2, 3, 4
	0x6d, 0x7d, 0x27, 0x7f, 0x6f,		// 5, 6, 7, 8, 9
	0x77, 0x7C, 0x39, 0x5E, 0x79,		// A, B, C, D, E
	0x71								// F
};

const UINT8 Alphabet_pattern[26] = 
{	// 18:88 LED Numeric,Character Display
	// Big Alphabet			0x41
	0x77, 0x7C, 0x39, 0x5E, 0x79,		// A, B, C, D, E
 	0x71, 0x6F, 0x76, 0x06, 0x1F,		// F, G, H, I, J
 	0x00, 0x38, 0x00, 0x54, 0x3F,		// K, L, M, N, O
 	0x73, 0x67, 0x50, 0x6D, 0x78,		// P, Q, R, S, T
 	0x3E, 0x00, 0x00, 0x00, 0x66,		// U, V, W, X, Y
 	0x08, 				// Z
};

void Cmp_Display_Service(T_DispInfo* psDisplayInfo)
{
	if(ERROR_State == NO_ERROR)
	{
		Cmp_Dsp_Setting_Normal(psDisplayInfo->DispType, (*(UINT32*)(0x0005FF02)));
	}
	else
	{
		Cmp_Dsp_Setting_Error();
	}

	if(DataReadWriteDataMemory(mucDispEnable, READ, NA))
	{
		Cmp_Dsp_Displaying();
	}
	else
	{
		gu8LED_Data[0].word = NumbertoChar(FCT_JIGVersion % 10);
		gu8LED_Data[1].word = NumbertoChar(FCT_JIGVersion / 10);
		gu8LED_Data[2].word = NumbertoChar('U');
		gu8LED_Data[3].word = CLEAR;
	}
}

void Cmp_Dsp_Setting_Normal(UINT8 ucDispType, UINT16 ucCheckSum)
{
	UINT8 TEMP;

	memset(gu8Temp_Data,NULL,SIZE_OF_ARRAY(gu8Temp_Data));
	
	gu8Temp_Data[0] = NumbertoChar(ucDispType % 10);							//	Show Platform
	gu8Temp_Data[1] = NumbertoChar(ucDispType / 10);							//	Show Platform
	
	if(CHECKSUM_KEY == BIN4(1100) && Get_JigKey() == JIG_CONF_KEY)
	{
		TEMP = (ucCheckSum / 1);
		gu8Temp_Data[0] = NumbertoChar(TEMP % 16);
		TEMP = (ucCheckSum / 16);
		gu8Temp_Data[1] = NumbertoChar(TEMP % 16);
		TEMP = (ucCheckSum / 256);
		gu8Temp_Data[2] = NumbertoChar(TEMP % 16);
		TEMP = (ucCheckSum / 4096);
		gu8Temp_Data[3] = NumbertoChar(TEMP % 16);
	}
	else
	{		
		if(ucDispType == JigUpdate && Get_JigKey() == JIG_CONF_KEY)	
		{
			gu8Temp_Data[0] = NumbertoChar(GET_LOW_1BYTE(FCT_JIGVersion));
			gu8Temp_Data[1] = NumbertoChar(GET_HIGH_1BYTE(FCT_JIGVersion));
			gu8Temp_Data[2] = NumbertoChar('U');
		}
		else if(ucDispType == Vivace && Onboarding_ModelFlag == SET)
		{
			gu8Temp_Data[2] = NumbertoChar('P');
		}
		else
		{	;
		}
	}
}

void Cmp_Dsp_Setting_Error()
{
	if(ERROR_State == SE_ERROR)
	{
		gu8Temp_Data[0] = NumbertoChar('E');
		gu8Temp_Data[1] = NumbertoChar('S');
		gu8Temp_Data[2] = NULL;
		gu8Temp_Data[3] = NULL;
	}	
	else
	{
		gu8Temp_Data[0] = NumbertoChar(4);
		gu8Temp_Data[1] = NumbertoChar('E');
		gu8Temp_Data[2] = NumbertoChar('D');
		gu8Temp_Data[3] = NULL;
	}
}

void Cmp_Dsp_Displaying()
{	
	if(Get_JigKey() == JIG_CONF_KEY && ERROR_State == NO_ERROR)
	{		
		if(CHECKSUM_KEY == BIN4(1100))
		{
			gu8LED_Data[0].word = Lamp1sBlink(gu8Temp_Data[0],gu8Temp_Data[2]);
			gu8LED_Data[1].word = Lamp1sBlink(gu8Temp_Data[1],gu8Temp_Data[3]);
			gu8LED_Data[2].word = Lamp1sBlink(NumbertoChar('L'),NumbertoChar('H'));
			gu8LED_Data[3].word = NULL;
		}
		else
		{
			gu8LED_Data[0].word = gu8Temp_Data[0];
			gu8LED_Data[1].word = gu8Temp_Data[1];
			gu8LED_Data[2].word = gu8Temp_Data[2];
			gu8LED_Data[3].word = gu8Temp_Data[3];
		}
	}	
	else
	{	
		gu8LED_Data[0].word = Lamp1sBlink(gu8Temp_Data[0],NULL);
		gu8LED_Data[1].word = Lamp1sBlink(gu8Temp_Data[1],NULL);
		gu8LED_Data[2].word = Lamp1sBlink(gu8Temp_Data[2],NULL);
		gu8LED_Data[3].word = Lamp1sBlink(gu8Temp_Data[3],NULL);
	}
}


UINT8 Lamp1sBlink(UINT8 ucblinkOn, UINT8 ucblinkOff)
{
	return (timer_1s_calc() % 2 == 1 ? ucblinkOn : ucblinkOff);
}

UINT8 NumbertoChar(UINT8 ucnumber)
{
	return (ucnumber < 16 ? Number_pattern[ucnumber] : Alphabet_pattern[ucnumber - 65]);
}





/***********************************************************
 End File
************************************************************/
